$(function () {
    $('#save').click(function (e) {
        e.preventDefault();
        var publisher_id = $(this).attr('publisher_id');
        var type = $(this).attr('type');
        var url = '';
        if (type == 1) {
            url = '/admin/mod_publisher/'
        } else {
            url = '/admin/add_publisher/'
        }
        var name = $('#publisher-name').val();
        var address = $('#address').val();
        zlajax.post({
            'url': url,
            'data': {
                'name': name,
                'address': address,
                'publisher_id': publisher_id
            },
            'success': function (data) {
                if (data['code'] == 200) {
                    xtalert.alertSuccessToast('操作成功！')
                } else {
                    xtalert.alertInfo(data['message'])
                }
            },
            'fail': function () {
                xtalert.alertNetworkError()
            }
        })
    });

    $('.mod-btn').click(function (e) {
        e.preventDefault();
        var info = $(this).parent().siblings();
        var publisher_id = info.eq(0).html();
        var name = info.eq(1).html();
        var address = info.eq(2).html();

        $('#save').attr({
            "type": 1,
            "publisher_id": publisher_id
        });

        $('#publisher-name').val(name);
        $('#address').val(address);

    });
    $('.del-btn').click(function (e) {
        e.preventDefault();
        var info = $(this).parent().siblings();
        var publisher_id = info.eq(0).html();
        xtalert.alertConfirm({
            'title': '作者及图书都会被删除，确定吗',
            'confirmCallback': function () {
                zlajax.get({
                    'url': '/admin/del_publisher/',
                    'data': {
                        'publisher_id': publisher_id
                    },
                    'success': function (data) {
                        if (data['code'] == 200) {
                            xtalert.alertSuccessToast('删除成功！');
                            setTimeout(function () {
                                window.location.reload()
                            }, 800)
                        } else {
                            xtalert.alertInfo(data['message'])
                        }
                    },
                    'fail': function () {
                        xtalert.alertNetworkError()
                    }
                })
            }
        })
    });
});